//
//  CLMainViewController.m
//  chanlin
//
//  Created by Ryan on 14-11-1.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLMainViewController.h"
#import "BlockUI.h"
#import "CLNavigationController.h"
#import "NSObject+kit.h"
#import "CLPushAnimatedRight.h"
#import "CLGloble.h"
#import "IfengOrderViewController.h"
#import "CLPushAnimatedTop.h"
#import "CLGlobleDataModel.h"
#import "CLLoginModel.h"
#import "CLNavViewController.h"
#import "UIView+Utils.h"

#import "CLIndexViewController.h"
#import "EColumViewController.h"
#import "IFAudioPlayerManager.h"
@interface CLMainViewController()

@property (nonatomic,strong) CLIndexViewController *indexView;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, strong) IfengOrderViewController *order;
@property (nonatomic,strong) UIButton *orderClickBtn;

@property (nonatomic,strong)UIView *status;

@end

@implementation CLMainViewController

- (instancetype)init{
    self=[super init];
    if(self){
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;

    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [[IFAudioPlayerManager shareInstance] notHiddenStateWindwo];
    [super viewWillAppear:animated];
    
    if (![_backGroundView.superview isEqual:(CLMainViewController *)self.view]) {
        [UIView animateWithDuration:0.35 animations:^{
            [self.orderBtn setImage:[UIImage imageNamed:@"arrow_open_order.png"] forState:UIControlStateNormal];
            [self.view addSubview:_backGroundView];
        } completion:^(BOOL finished) {
            
        }];
    }

}

- (void)viewWillDisappear:(BOOL)animated{
    [[IFAudioPlayerManager shareInstance] hiddenStateWindow];
    
}


- (void)viewDidLoad{
    
    self.status=[UIView new];
    self.status.backgroundColor=RGB(0xf8, 0xf8, 0xf8);
    self.status.frame=CGRectMake(0, 0, GlobleWidth, 20.f);
    
    
    _backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GlobleWidth, 65.f)];
    _backGroundView.userInteractionEnabled = YES;
    _backGroundView.backgroundColor = RGB(0xf8, 0xf8, 0xf8);
    
    
    
    
    _topScrollView = [[ChannnelTopScrollView alloc] initWithDataSource:[ChannelDataSource new]];
    _topScrollView.scrollsToTop = NO;
    //_topScrollView.backgroundColor=[UIColor blueColor];
    [_backGroundView addSubview:_topScrollView];
    
    
    UIView *sepraretLine = [[UIView alloc]initWithFrame:CGRectMake(0, _backGroundView.frame.size.height-0.5, GlobleWidth, 0.5)];
    sepraretLine.backgroundColor = RGB(0xc9, 0xc9, 0xc9);
    [_backGroundView addSubview:sepraretLine];
    
    _controllerScrollView = [[ChannnelControllerScrollView alloc] initWithDataSource:[ChannelDataSource new]];
    _controllerScrollView.scrollsToTop = NO;
    [self.view addSubview:_controllerScrollView];
    
//    UILabel *label=[[UILabel alloc] init];
//    label.textColor=[UIColor blackColor];
//    label.text=[CLGloble sharedInstance].magazineName;
//    label.font=[UIFont fontWithName:@"Helvetica-Bold" size:20.f];
//    label.textAlignment=NSTextAlignmentCenter;
//    label.frame=CGRectMake(50.f, 20.f, GlobleWidth-100.f, 45.f);
//    [_backGroundView addSubview:label];
    
    [self.view addSubview:_backGroundView];
    
//    self.indexView=[CLIndexViewController new];
//    self.indexView.view.frame=CGRectMake(0.f, 65.f, GlobleWidth, GlobleHeight-65.f);
//
//    [self.view addSubview:self.indexView.view];
    
    self.view.backgroundColor=[UIColor whiteColor];
//    [self.indexView setNewsCategory:@{@"channelId":[CLGloble sharedInstance].magazineId,
//                                      @"url":[NSString stringWithFormat:@"http://qsp.onesed.cn/e/app/book.php?id=%@",[CLGloble sharedInstance].magazineId],
//                                      @"num":@"new"}];
    
    
    
    
//    UIView *r=[[UIView alloc] initWithFrame:CGRectMake(self.orderBtn.right, 27.f, 10.f, 30.f)];
//    r.backgroundColor=RGB(0xf8, 0xf8, 0xf8);
//    [_backGroundView addSubview:r];
    
    self.rightMark=[[UIImageView alloc] initWithFrame:CGRectMake(self.orderBtn.left-10, 20, 30.f, 44.f)];
    self.rightMark.image=[UIImage imageNamed:@"index_right.png"];
    [_backGroundView addSubview:self.rightMark];

    
    
    
    
    self.logoBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.logoBtn.frame=CGRectMake(6.f, 18.f, 28.f, 56.f);
   
    [self.logoBtn setImage:[[CLGloble sharedInstance] getTodyLogo] forState:UIControlStateNormal];
    
    self.leftMark=[[UIImageView alloc] initWithFrame:CGRectMake(self.logoBtn.right-15, 20, 30.f, 44.f)];
    self.leftMark.image=[UIImage imageNamed:@"index_left.png"];
    [_backGroundView addSubview:self.leftMark];
    
    [_backGroundView addSubview:self.logoBtn];
    __weak typeof(self) me=self;
    [self.logoBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.panpushAnimation pushDirectly];
    }];
    
    
    UIButton *orderClickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    orderClickBtn.frame = CGRectMake(GlobleWidth-30, 27.f, 30, 30);
    [orderClickBtn setBackgroundColor:RGB(0xf8, 0xf8, 0xf8)];
    [orderClickBtn addTarget:self action:@selector(clickOrderBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_backGroundView addSubview:orderClickBtn];
    self.orderClickBtn=orderClickBtn;
    
    
    self.orderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.orderBtn.alpha = 1;
    [_orderBtn setFrame:CGRectMake(8, 8, 14, 14)];
    _orderBtn.userInteractionEnabled=NO;
    // self.orderBtn.contentEdgeInsets=UIEdgeInsetsMake(5.f, 5.f, 5.f, 5.f);
    [self.orderBtn setImage:[UIImage imageNamed:@"arrow_open_order.png"] forState:UIControlStateNormal];
    self.orderBtn.backgroundColor = [UIColor clearColor];
    
    
    
    _isOpen = NO;
    
    /*[self.orderBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        
        
        //原来的
        _order =[IfengOrderViewController new];
        _order.view.frame = CGRectMake(0, 0, GlobleWidth, GlobleHeight);
     
        [UIView animateWithDuration:0.35 animations:^{
            [self.orderBtn setImage:[UIImage imageNamed:@"arrow_close_order.png"] forState:UIControlStateNormal];
            [_order.view addSubview:_backGroundView];
        } completion:^(BOOL finished) {
            
        }];
        
        [self.view addSubview:_order.view];
        
        [[CLPushAnimatedTop sharedInstance] pushController:_order];
     
    }];*/
    [orderClickBtn addSubview:_orderBtn];
    
   
//    [_backGroundView bringSubviewToFront:_logoBtn];
    
    if(!self.panpushAnimation){
        self.panpushAnimation=[PanPushAnimationNavLeft sharedInstance];
        //self.panpushAnimation.linkType=@"navLeft";// settings
       // self.panpushAnimation.animationType=Dispatch_AnimationType_presentFromLeftNav;
       // self.panpushAnimation.pathName=@"navLeft";// navLeft
       // self.panpushAnimation.backTarget=self;
        CLNavViewController *nav=[CLNavViewController alloc];
        [self.panpushAnimation addNextControllerByType:nav];
    }

    [_controllerScrollView.panGestureRecognizer addTarget:self action:@selector(doPanGesture:)];

}

- (void)reUseBtn{
    self.orderClickBtn.enabled=YES;
}


#pragma --mark 订阅频道
- (void)clickOrderBtn:(UIButton *)button {
    self.orderClickBtn.enabled=NO;
    [self performSelector:@selector(reUseBtn) withObject:nil afterDelay:1];
    

    if (!_isOpen) {
        _order =[IfengOrderViewController new];
        _order.view.frame = CGRectMake(0, 0, GlobleWidth, GlobleHeight);
        
        [UIView animateWithDuration:0.2 animations:^{
            self.orderBtn.transform = CGAffineTransformMakeRotation((-179.9f * M_PI) / 180.0f);
            [_order.view addSubview:_backGroundView];
        } completion:^(BOOL finished) {
        }];
        
        
      
        [[self k_mainWindow] addSubview:_backGroundView];
        [self.view addSubview:_order.view];
       // [[self k_mainWindow] addSubview:self.status];
        
        [[CLPushAnimatedTop sharedInstance] pushController:_order];
    }else{
        
        [UIView animateWithDuration:0.2 animations:^{
            [_order.view addSubview:_backGroundView];
            [self.view bringSubviewToFront:_backGroundView];
            self.orderBtn.transform = CGAffineTransformIdentity;
        }];
        
        __weak typeof(self)me=self;
        [[CLPushAnimatedTop sharedInstance] popTop:^(BOOL finish) {
       
            [me.view addSubview:me.backGroundView];
          //  [me.status removeFromSuperview];
        } before:^{
             [[me k_mainWindow] addSubview:me.backGroundView];
        }] ;

    }
    _isOpen = _isOpen?NO:YES;
}


- (void)doPanGesture:(UIPanGestureRecognizer *)recognizer {
    
    
    if (_controllerScrollView.contentOffset.x <= 0){
        // CGPoint p=[recognizer velocityInView:self];
       // cactusAppDelegate *delegate = (cactusAppDelegate *)[UIApplication sharedApplication].delegate;
        
        // 拖出设置页
        [self.panpushAnimation doAnimation:recognizer];
        //[Globle shareInstance].willPopToSet = YES;
        
        
    }else{
               [self.panpushAnimation cancleImmdiatelay];
       
    }
}



@end
